/**
 * 首页的控制部分
 */
$(function(){
    	 paint();
    	 $('#footer_toggle').click(function(){
    		 $(this).attr('class')=="footer_up" ? 
    				  ($(this).attr('class','footer_down'),
    			       $('footer div').css('height','100%'),
    			       $('footer div.right_div').find('label:eq(0)').css('display','none'),
    			       $('footer div.right_div').find('label:eq(1)').css('display','block')
    			      )
    				 :($(this).attr('class','footer_up'),$('footer div').css('height','4.5em'),
    				    $('footer div.right_div').find('label:eq(0)').css('display','block'),
    	    			$('footer div.right_div').find('label:eq(1)').css('display','none'));
    		 
    	 });
    	  
    	   $('.rbcontainer a.search_info').click(function(){
     	      paint_select(user_id);
     		   return false;
     	   });
     	   $('#myClass button').click(function(){
     		   $('#myClass').hide('slow');
     	   });
     	   $('#myClass img').click(function(){
     		   $('#myClass').hide('slow');
     	   });
     });

	 function paint(){
		 $.ajax({
			 url:'/WDDXSERVER/home/getDate',
			 data:{userid:user_id},
			 type:'post'
		 }).done(function(data){
		
			 var $node=$('div.main_rtcontainer').find('img');
			 var str='<span>'+data.xqj_en+'</span><span>'+data.xqj+'</span>'+
             '<ul><li><h3>'+data.djz+'</h3></li><li><h3>'+data.xqj_zhcn+'</h3></li><li><h3>'+data.year_month_day+'</h3></li></ul>'+
             '<ul><li>'+data.nl+'</li><li>宜：'+data.y+'</li><li>忌：'+data.j+'</li></ul>';
			 $node.after(str);
			 $("#iossmall,#iosbig").attr("src","/WDDXSERVER/home/CreateQrCode?ticket="+ticket+"&device=ios&radom="+Math.random());
			 $("#androidsmall,#androidbig").attr("src","/WDDXSERVER/home/CreateQrCode?ticket="+ticket+"&device=android&radom="+Math.random()); 
		 });
		 $.getJSON('/WDDXSERVER/message/getNewMessage?callback=?',{userid:user_id,ticket:ticket},function(data){
			var $node=$('div.main_rbcontainer div.message_header');
			$node.find('a:eq(0)').html('<h3> 消息 </h3> ( '+data.content.message.content.length+' )');
			var the_data=data.content.message.content;
			var length=the_data.length;
			var str=[];
			for(var i=0;i<length;i++){
				if(the_data[i].pic){
					str[i]='<li><a href="javascript:;"><img src="'+the_data[i].pic+'" /><span>'+the_data[i].n_title+'</span><span>'+the_data[i].n_send_time+'</span></a></li>'; 
				}else{
					str[i]='<li><a href="javascript:;"><img src="img/no_pic.png" /><span>'+the_data[i].n_title+'</span><span>'+the_data[i].n_send_time+'</span></a></li>'; 
				}
			}
			$node.after('<ul>'+str.join('')+'</ul>');
		 });
		 paint_functions({userid:user_id,ticket:ticket},'/WDDXSERVER/home/getUserFunction');
		
	 };

	 function paint_functions(data,url){
		 $('div.footer_left').empty();$('div.my_function').empty();$('div.other_function').empty();
		 $('div.footer_left').append('<a href="" id="function_add"><img  src="/WDDXSERVER/img/add.png" /></a>');
		 $('div.my_function').append('<p style="margin-top:-1.5em">我的应用</p>');
		 $('div.other_function').append('<p>其他应用</p>');
		  $.ajax({
			 url:url,
			 data:data,
			 traditional: true,
			 type:'post'
		  }).done(function(data){
			  for(var i9=0;i9<data.owned.length;i9++)
			     $('.footer_left a#function_add').before('<a href="javascript:;" path="'+data.owned[i9].function_path+
							 '" data-id="'+data.owned[i9].function_id+'" data-reveal-id="myModal"><img  src="'+data.owned[i9].function_face+'" /><label>'+data.owned[i9].function_name+'</label></a>');
	             $('#function_add').unbind('click').click(function(){
	            	 paint_function_dialog({userid:user_id,ticket:ticket},'/WDDXSERVER/home/getUserFunction');
		    		 return false;
		    	 });
	             $(".footer_left > a").click(function(e){
	            	 a_click($(this));
	             })
		  });
	 };
	 function paint_function_dialog(data,url){
		 $('div#myFunction div.the_functions ').remove();
		 $.ajax({
			 url:url,
			 data:data,
			 traditional: true,
			 type:'post'
		  }).done(function(data){
			  for(var i3=0;i3<data.owned.length;i3++){
					$('div#myFunction div.my_function p').after('<div class="the_functions" data-value="'+data.owned[i3].function_id+'">'+
					'<img  src="'+data.owned[i3].function_face+'" /><label></label><a href="'+data.owned[i3].function_path+'" onclick="return false">'+data.owned[i3].function_name+'</a></div>');
			  }
	          for(var i4=0;i4<data.remainder.length;i4++){
	            	$('div#myFunction div.other_function p').after('<div class="the_functions" data-value="'+data.remainder[i4].function_id+'">'+
	 				'<img  src="'+data.remainder[i4].function_face+'" /><label></label><a href="'+data.remainder[i4].function_path+'" onclick="return false">'+data.remainder[i4].function_name+'</a></div>');
			 }
		 $('#myFunction').show('slow');
	     controls('my_function','other_function');
	     controls('other_function','my_function');
	   
		 $('button.function_finish,div#myFunction h3 i.fa-times').unbind('click').click(function(e){
			 var $my_functions=$('#myFunction div.my_function ');
			 var my_function_length=$my_functions.find('div.the_functions').length;
			   var detial = new Array();
	            for(var i6=0;i6<my_function_length;i6++){
		           var detail_row = new Object();
		           detail_row.function_id = $my_functions.find('div.the_functions:eq('+i6+')').attr('data-value');
		           detail_row.function_name = $my_functions.find('div.the_functions:eq('+i6+')').find('a').text();
		           detail_row.function_face = $my_functions.find('div.the_functions:eq('+i6+')').find('img').attr('src');
		           detail_row.function_path = $my_functions.find('div.the_functions:eq('+i6+')').find('a').attr('href');
		           detial.push(detail_row);
		         }
			 $('#myFunction').hide('fast');
			 $.ajax({
			    url:'/WDDXSERVER/home/setUserFunction',
			    data:{userid:user_id,function_list:JSON.stringify(detial),ticket:ticket}
			 });
			 e.stopPropagation();
		    });
		     dialog_move($('div#myFunction div.my_dialog'));
		 });
	 }
	 function controls(class_name1,class_name2){
		 $('#myFunction .'+class_name1).find('label').unbind('click');
		 $('#myFunction .'+class_name1).find('label').click(function(event){
//			 paint_functions({userid:user_id,ticket:ticket},'/WDDXSERVER/home/getUserFunction');
			 $(this).unbind('click').parent('div').appendTo('#myFunction .'+class_name2);
			 if(class_name1=='my_function')
			     $('.footer_left ').find('a[data-id='+$(this).unbind('click').parent('div').attr('data-value')+']').remove();
			 else
				 $('.footer_left a#function_add').before('<a href="javascript:;" path="'+$(this).unbind('click').parent('div').find('a').attr('href')+
						 '"  data-id="'+$(this).unbind('click').parent('div').attr('data-value')+ 
						 '" data-reveal-id="myModal" ><img  src="'+$(this).unbind('click').parent('div').find('img').attr('src')+'" /><label>'+
						 $(this).unbind('click').parent('div').find('a').text()+'</label></a>');
			 controls(class_name2,class_name1);
				 $(".footer_left > a").unbind('click').click(function(e){
	            	 a_click($(this));
	             });	  
			 });
	 };
	 function the_scroll(length){
		 var the_length=length;
		 var $the_span=$('div.message_scroll').find('span:eq(0)');
		 while(the_length){
			 $the_span.delay(3000).animate({'margin-left':'-=100'+'%'},2000);
			 the_length--;
		 } 
			 $the_span.delay(3000).animate({'margin-left':'0'},2000);
	   };

	      function dialog_move(object){
	    		object.hover(function(){
	    			$(this).css('cursor','move');
	    		},function(){
	    			$(this).css('cursor','default');
	    		});
	    		object.mousedown(
	    		            function(event){
	    		    	//	$(this).css('cursor','move');
	    		                var isMove = true;
	    		                var screenW =$(document).width(); 
	    		                var screenH = $(document).height();
	    		                var dialogW=object.outerWidth(true);
	    		                var dialogH=object.outerHeight(true);
	    		                var abs_x = event.pageX - object.offset().left; 
	    		                var abs_y = event.pageY - object.offset().top;
	    		    			
	    		                $(document).mousemove(function (event) {  
	    		                        if (isMove) {  
	    		                            var obj = object; 
	    		                            var left_x = event.pageX - abs_x;
	    		                            var top_y = event.pageY - abs_y;
	    		    					//	console.log(top_y);
	    		                            if((screenW-dialogW)<=left_x){
	    		                                left_x = screenW-dialogW;
	    		                            }
	    		                            if((screenH-dialogH)<=top_y){
	    		                                top_y = screenH-dialogH;
	    		                            }
	    		                            if(left_x<0){
	    		                                left_x=20;
	    		                            }
	    		                            if(top_y<0){
	    		                                top_y=10;
	    		                            }
	    		    						
	    		                            obj.css({'left':left_x, 'top':top_y});  
	    		                        }  
	    		                    }
	    		                 ).mouseup(  
	    		                                function () {  
	    		                                	
	    		                                       isMove = false;  
	    		                                    }  
	    		                                );
	    		            }
	    		        );
	    	};